﻿using SharpDX;
using SharpDX.Direct2D1;

using System;
using System.Collections.Generic;
using System.Text;

namespace ModelCreator
{
    class GeometryConstructor
    {
        private PathGeometry geometry = null;
        private List<Vector2> Points = new List<Vector2>();
        private Vector2 FloatingPoint;
        private Factory factory;
        private RenderTarget renderTarget;

        public GeometryConstructor()
        {
        }

        public void Initialization(RenderTarget renderTarget, Factory factory)
        {
            this.factory = factory;
            this.renderTarget = renderTarget;
            geometry = new PathGeometry(factory);
        }

        public void SetPoint(Vector2 point)
        {
            Points.Add(point);
            FloatingPoint = point;
            RecreateGeometry();
        }

        private void RecreateGeometry()
        {
            if (Points.Count > 0)
            {
                geometry = new PathGeometry(factory);
                var sink = geometry.Open();
                sink.BeginFigure(new DrawingPointF(Points[0].X, Points[0].Y), FigureBegin.Hollow);
                for (int i = 1; i < Points.Count; i++)
                {
                    sink.AddLine(new DrawingPointF(Points[i].X, Points[i].Y));
                }
                sink.AddLine(new DrawingPointF(FloatingPoint.X, FloatingPoint.Y));
                sink.EndFigure(FigureEnd.Open);
                sink.Close();
            }
        }

        public void SetFloatingPoint(Vector2 point)
        {
            FloatingPoint = point;
            RecreateGeometry();
        }

        public void Draw(SolidColorBrush brush)
        {
            if (Points.Count > 0)
                renderTarget.DrawGeometry(geometry, brush);
        }

        public void Interrupt()
        {
            //throw new NotImplementedException();
        }
    }
}
